<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28 0028
 * Time: 下午 2:33
 */

namespace app\sports_admin\controller;

use app\sports_admin\model\ConstitutionType as ConstitutionTypeModel;
use app\sports_admin\model\ConstitutionType;
use app\sports_admin\vaildate\ConstitutionProject as ConstitutionProjectVaildate;
use app\sports_admin\vaildate\ConstitutionType as ConstitutionTypeVaildate;
use app\sports_admin\service\ConstitutionProject as ConstitutionProjectService;
use app\sports_admin\model\ConstitutionProject as ConstitutionProjectModel;
use app\sports_admin\model\Grade as GradeModel;
use think\Request;

class ConstitutionProject extends Base
{
    //首页
    public function index()
    {
        //类别
        $info = ConstitutionTypeModel::select();
        //年级
        $grade = GradeModel::select();
        return view('index',compact('info','grade'));
    }
    //列表
    public function projectList()
    {
        return ConstitutionProjectService::projectList();
    }
    //添加页面
    public function add()
    {
        //类别
        $info = ConstitutionTypeModel::select();
        //年级
        $grade = GradeModel::select();
        //年份
        $year = date("Y",time());
        return view('add',compact('info','grade','year'));
    }
    //添加
    public function insert()
    {
        (new ConstitutionProjectVaildate())->insert();
        return ConstitutionProjectService::insert();
    }
    //编辑
    public function edit()
    {
        $params = Request::instance()->param();
        //类别
        $info = ConstitutionTypeModel::select();
        //年级
        $grade = GradeModel::select();
        $data = ConstitutionProjectModel::where('grade_id',$params['grade_id'])->where('type_id',$params['type_id'])
            ->where('sex',$params['sex'])->where('year',$params['year'])->select();
        $year = $params['year'];
        $type = $params['type_id'];
        $sex = $params['sex'];
        $grade_id = $params['grade_id'];
        $count = count($data);
        return view('edit',compact('info','grade','data','year','type','sex','grade_id','count'));
    }
    //更新
    public function update()
    {
        (new ConstitutionProjectVaildate())->update();
        return ConstitutionProjectService::update();
    }
    //删除
    public function delete()
    {
        (new ConstitutionProjectVaildate())->delete();
        return ConstitutionProjectService::delete();
    }
    //类别首页
    public function typeIndex()
    {
        return view('type_index');
    }
    //类别列表
    public function typeList()
    {
        return ConstitutionProjectService::typeList();
    }
    //类别添加页面
    public function typeAdd()
    {
        $grade = GradeModel::select();
        return view('type_add',compact('grade'));
    }
    //类别添加
    public function typeInsert()
    {
        (new ConstitutionTypeVaildate())->insert();
        return ConstitutionProjectService::typeInsert();
    }
    //类别编辑
    public function typeEdit()
    {
        $params = Request::instance()->param();
        $grade = GradeModel::select();
        $info = ConstitutionTypeModel::where('id',$params['id'])->find();
        $gradeArray = explode(',',trim($info->grade_id,','));
        return view('type_edit',compact('grade','gradeArray','info'));
    }
    //类别修改
    public function typeUpdate()
    {
        (new ConstitutionTypeVaildate())->update();
        return ConstitutionProjectService::typeUpdate();
    }
    //删除类别
    public function typeDelete()
    {
        (new ConstitutionTypeVaildate())->go_check();
        return ConstitutionProjectService::typeDelete();
    }
}